using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    public struct PlayerData
    {
        public Vector2 Position;
        public bool isAlive;
        public Color Color;
        public float Angle;
        public float Power;
    }
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GraphicsDevice device;

        Texture2D backgroundtexture;
        Texture2D foregroundtexture;
        Texture2D carriageTexture;
        Texture2D cannonTexture;
        SpriteFont font;

        int screenWidth;
        int screenHeight;
        float playerScaling;
        int currentPlayer;

        PlayerData[] players;
        int numofPlayers = 4;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 500;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "My game";

            base.Initialize();
        }

        private void SetUpPlayers()
        {
            Color[] playerColors = new Color[10];
            playerColors[0] = Color.Red;
            playerColors[1] = Color.Green;
            playerColors[2] = Color.Blue;
            playerColors[3] = Color.Purple;
            playerColors[4] = Color.Orange;
            playerColors[5] = Color.Indigo;
            playerColors[6] = Color.Yellow;
            playerColors[7] = Color.SaddleBrown;
            playerColors[8] = Color.Tomato;
            playerColors[9] = Color.Turquoise;

            players = new PlayerData[numofPlayers];

            for (int i = 0; i < numofPlayers; i++)
            {
                players[i].isAlive = true;
                players[i].Color = playerColors[i];
                players[i].Angle = MathHelper.ToRadians(90);
                players[i].Power = 100;
            }

            players[0].Position = new Vector2(100, 193);
            players[1].Position = new Vector2(200, 212);
            players[2].Position = new Vector2(300, 361);
            players[3].Position = new Vector2(400, 164);


        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            backgroundtexture = this.Content.Load<Texture2D>("background");
            foregroundtexture = this.Content.Load<Texture2D>("foreground");
            carriageTexture = this.Content.Load<Texture2D>("carriage");
            cannonTexture = this.Content.Load<Texture2D>("cannon");
            font = this.Content.Load<SpriteFont>("myFont");

            device = graphics.GraphicsDevice;
            screenWidth = device.PresentationParameters.BackBufferWidth;
            screenHeight = device.PresentationParameters.BackBufferHeight;
            SetUpPlayers();
            playerScaling = 40.0f / (float)carriageTexture.Width;
            // TODO: use this.Content to load your game content here
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }


        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            ProcessKeyboard();
            base.Update(gameTime);
        }

        public void ProcessKeyboard()
        {
            KeyboardState keyboardstate = Keyboard.GetState();
            //angle
            if (keyboardstate.IsKeyDown(Keys.Left))
            {
                players[currentPlayer].Angle -= 0.1f;
            }
            if (keyboardstate.IsKeyDown(Keys.Right))
            {
                players[currentPlayer].Angle += 0.1f;
            }

            if (players[currentPlayer].Angle > MathHelper.PiOver2)
            {
                players[currentPlayer].Angle = -MathHelper.PiOver2;
            }
            if (players[currentPlayer].Angle < -MathHelper.PiOver2)
            {
                players[currentPlayer].Angle = MathHelper.PiOver2;
            }

            //power

            if(keyboardstate.IsKeyDown(Keys.Up))
            {
                players[currentPlayer].Power += 1;
            }
            if (keyboardstate.IsKeyDown(Keys.Down))
            {
                players[currentPlayer].Power -= 1;
            }
            if (keyboardstate.IsKeyDown(Keys.PageUp))
            {
                players[currentPlayer].Power += 20;
            }
            if (keyboardstate.IsKeyDown(Keys.PageDown))
            {
                players[currentPlayer].Power -= 20;
            }

            if (players[currentPlayer].Power > 1000)
            {
                players[currentPlayer].Power = 1000;
            }
            if (players[currentPlayer].Power < 0)
            {
                players[currentPlayer].Power = 0;
            }



        }

        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            DrawScenery();
            DrawPlayers();
            DrawText();
            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void DrawScenery()
        {
            Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
            spriteBatch.Draw(backgroundtexture, screenRectangle, Color.White);
            spriteBatch.Draw(foregroundtexture, screenRectangle, Color.White);
        }

        private void DrawPlayers()
        {
            foreach (PlayerData player in players)
            {
                if (player.isAlive)
                {
                    int xPos = (int)player.Position.X;
                    int yPos = (int)player.Position.Y;
                    Vector2 cannonOrigin = new Vector2(11,50);

                    spriteBatch.Draw(cannonTexture, new Vector2(xPos + 20, yPos - 10), null, player.Color, player.Angle, cannonOrigin, playerScaling, SpriteEffects.None, 1);
                    spriteBatch.Draw(carriageTexture, player.Position, null, player.Color, 0, new Vector2(0, carriageTexture.Height), playerScaling, SpriteEffects.None, 0);
                }
            }
        }

        private void DrawText()
        {
            PlayerData player = players[currentPlayer];
            int currentangle = (int)MathHelper.ToDegrees(player.Angle);
            spriteBatch.DrawString(font, "Cannon angle: " + currentangle.ToString(), new Vector2(20, 20), Color.White);
            spriteBatch.DrawString(font, "Cannon power: " + player.Power.ToString(), new Vector2(20, 45), Color.White);
        }


    }
    
  
     
}
